<?php

namespace app\api\controller;

use fast\Random;
use think\facade\Config;
use think\facade\Validate;
use app\common\library\Ems;
use app\common\library\Sms;
use app\common\controller\Api;
use app\common\model\Order;


/**
 * 生成订单接口.
 */
class Waporder extends Api
{
    protected $noNeedLogin = ['index','login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 获取单号
     */
    public function NumRule($prefix='DH')
    {
        return $prefix.date('YmdHis',time()).mt_rand(1000, 9999);
    }

    /**
     * 生成订单
     * @param $user_id int 用户id
     * @param $price string 订单金额
     * @param $pay_type int 支付类型  1:支付宝 2:微信
     * @param $game_id int 游戏id
     * @param $type string 商品类型  商品类型 1-x 九州 0-1红包
     * @param $service_id int 游戏id 充值游戏大区
     * @param $actorid int 角色id
     * @return array
     */
    public function index() //$user_id,$price,$pay_type,$game_id,$type,$service_id,$actorid
    {
        $param = $this->request->param();
        //请求参数不能为空
        if(empty($param['user_id']) || empty($param['price']) || empty($param['pay_type']) || empty($param['game_id'])  || empty($param['service_id']) || empty($param['actorid'])){
            $this->error(__('param is null'));
        }
//        dd($param);
        //获取用户信息
        $where['id'] = $param['user_id'];
        $userInfo = \app\common\model\User::get($where);
        if(empty($userInfo)){
            $this->error(__('用户不存在')); //账户不存在
        }

        //创建订单
        $order = new Order();
        $order_no = $this->NumRule('CZ'); //订单编号
        $order['order_no'] = $order_no;
        $order['user_id'] = $param['user_id'];   //用户id
        $order['date_day'] = date('Ymd',time());   //日期时间
        $order['price'] = $param['price'];   //金额
        $order['status'] = 0;   //支付状态
        $order['pay_type'] = $param['pay_type'];   //支付类型 1:支付宝 2:微信
        $order['title'] ='元宝充值';   //支付说明
        $order['game_id'] = $param['game_id'];   //游戏id 0:app 其他游戏
//        $order['type'] = $param['type'];   //商品类型 1-x 九州 0-1红包
        $order['create_time'] = time();   //创建时间
        $order['agent_id'] = $userInfo['agent_id'];   //代理id
        $order['p_agent_id'] = $userInfo['p_agent_id'];   //上级代理id
        $order['user_date'] = $userInfo['create_time'];   //用户注册日期
        $order['sex'] = $userInfo['sex'];   //用户性别
        $order['service_id'] = $param['service_id'];   //充值游戏大区
        $order['actorid'] = $param['actorid'];   //角色id


        $re = $order->save();
        //订单创建成功拉起支付
        if($re){
//            $data['url'] = "http://39.101.192.177/api/alindex/alipay?service_id=".$param['service_id']."&actorid=".$param['actorid']."&order_no=$order_no&price=".$param['price'];
            $url = "http://47.96.128.147/api/alindex/alipay?service_id=".$param['service_id']."&actorid=".$param['actorid']."&order_no=$order_no&price=".$param['price'];
            
            Header("HTTP/1.1 303 See Other");
            Header("Location: $url");
//            header("Location: $data['url']");
//            $this->success(__('create order success'), $data);
        }else{
            $this->error(__('create order failed'));
        }

    }

}
